function [R1final, R2final, C1final, C2final, Rafinal, Rbfinal, error]=obtainSallenKey (stage, stageType)
C=[0.47e-6, 1e-9, 1e-12, 1e-6, 1.2e-9, 1.2e-12, 1.5e-9, 1.5e-12, 1.8e-9, 1.8e-12, 10e-9, 10e-12, 10e-6, 100e-9, 100e-12, 100e-6, 1000e-12, 1000e-6, 12e-9, 12e-12, 120e-9, 120e-12, 15e-9, 150e-9, 150e-12, 18e-9, 18e-12, 180e-9, 180e-12, 2.2e-9, 2.2e-12, 2.2e-6, 2.7e-9, 2.7e-12, 22e-9, 22e-12, 22e-6, 220e-9, 220e-12, 220e-6, 2200e-6, 27e-9, 27e-12, 270e-9, 270e-12, 3.3e-9, 3.3e-12, 3.3e-6, 3.9e-9, 3.9e-12, 33e-9, 33e-12, 33e-6, 330e-9, 330e-12, 330e-6, 3300e-6, 39e-9, 39e-12, 390e-9, 390e-12, 4.7e-9, 4.7e-12, 4.7e-6, 47e-9, 47e-12, 47e-6, 470e-9, 470e-12, 470e-6, 4700e-6, 5.6e-9, 5.6e-12, 56e-9, 56e-12, 560e-9, 560e-12, 6.8e-9, 6.8e-12, 680e-9, 680e-12, 8.2e-9, 8.2e-12, 82e-9, 82e-12, 820e-12];
error=0;
i=1;    
m1(i)=0;
n1(i)=0;
n1f(i)=0;
m1f(i)=0;
Rf(i)=0;
Cf(i)=0;

Qc=abs(stage.stagePoles(1)/(2*real(stage.stagePoles(1))));

if stageType==2            
for m=0:0.1:100
    for n=0:0.1:100
        Q=(sqrt(m*n)/(m+1+m*n*(1-stage.stageGain)));
        if (Q>=0.98*Qc && Q<=1.02*Qc)
            n1(i)=n;
            m1(i)=m;
            i=i+1;
            break;
        end
    end
end
elseif stageType==3
for m=0:0.1:10
    for n=0:0.1:10
        Q=(sqrt(m*n)/(1+n+(1-stage.stageGain)*n*m));
        if (Q>=0.98*Qc && Q<=1.02*Qc)
            n1(i)=n;
            m1(i)=m;
            i=i+1;
            break;
        end
    end
end
end

mxn=n1.*m1;
i=1;

for t=1:length(C)
        for j=1:length(mxn)
            R=1/(abs(stage.stagePoles(1))*C(t)*sqrt(mxn(j)));
            if (R>10 && R<100000)
                ZC=1/(C(t)*(abs(stage.stagePoles(1))));
                if (R<10*ZC && R>ZC/10)
                n1f(i)=n1(j);
                m1f(i)=m1(j);
                Rf(i)=R;
                Cf(i)=C(t);
                i=i+1;
                break;
                end
            end
        end
end

i=1;

for Ra=logspace(2,4,50)
    Rb=(stage.stageGain-1)*Ra; 
    Raf(i)=Ra;
    Rbf(i)=Rb;
    i=i+1;
end
p=random('unid',length(Rf));
R1final=Rf(p);
R2final=Rf(p)*m1f(p);
C2final=Cf(p);
C1final=Cf(p)*n1f(p);
p=random('unid',length(Raf));
Rafinal=Raf(p);
Rbfinal=Rbf(p);
if n1(1)==0 && m1(1)==0
    error=1;
end
end 